package com.liuqi.openai.model.output;

import static com.liuqi.openai.util.ValidationUtil.ensureNotNull;

/**
 * 表示来自各种类型模型的响应，包括文本生成、聊天、向量化
 *
 * @author liuqi
 * @date 2025/7/19
 **/
public class Response<T> {

    /**
     * AI响应内容
     */
    private final T content;

    /**
     * tokens 计量
     */
    private final TokenUsage tokenUsage;

    /**
     * finish_reason
     */
    private final FinishReason finishReason;

    public Response(T content, TokenUsage tokenUsage, FinishReason finishReason) {
        this.content = ensureNotNull(content, "content");
        this.tokenUsage = tokenUsage;
        this.finishReason = finishReason;
    }

    public T content() {
        return content;
    }

    public TokenUsage tokenUsage() {
        return tokenUsage;
    }

    public FinishReason finishReason() {
        return finishReason;
    }

    public static <T> Response<T> from(T content) {
        return new Response<>(content, null, null);
    }

    public static <T> Response<T> from(T content, TokenUsage tokenUsage) {
        return new Response<>(content, tokenUsage, null);
    }

    public static <T> Response<T> from(T content, TokenUsage tokenUsage, FinishReason finishReason) {
        return new Response<>(content, tokenUsage, finishReason);
    }

    @Override
    public String toString() {
        return "Response{" +
                "content=" + content +
                ", tokenUsage=" + tokenUsage +
                ", finishReason=" + finishReason +
                '}';
    }
}
